package com.wechart.service.statistics.impl;

import com.wechart.dao.domain.Orgnetinfo;
import com.wechart.dao.domain.OrgnetinfoCriteria;
import com.wechart.dao.domain.PrtryinfoCriteria;
import com.wechart.dao.mapper.OrgnetinfoMapper;
import com.wechart.dao.mapper.PrtryinfoMapper;
import com.wechart.model.OrgNetInfoPrint;
import com.wechart.model.StatisticsModel;
import com.wechart.service.statistics.OrgnetinfoService;
import com.wechart.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service(value = "orgnetinfoService")
public class OrgnetinfoServiceImpl implements OrgnetinfoService {

    @Autowired
    private OrgnetinfoMapper orgnetinfoMapper;

    @Autowired
    private PrtryinfoMapper prtryinfoMapper;



    @Override
    public List<Orgnetinfo> getOrgnetinfo(StatisticsModel statisticsModel){
        OrgnetinfoCriteria orgnetinfoCriteria=new OrgnetinfoCriteria();
        OrgnetinfoCriteria.Criteria criteria=orgnetinfoCriteria.createCriteria();
        if(StringUtils.isNoNullString(statisticsModel.getStart())){
            criteria.andWorkdateGreaterThan(StringUtils.cutDateStr(statisticsModel.getStart()));
        }
        if(StringUtils.isNoNullString(statisticsModel.getEnd())){
            criteria.andWorkdateLessThan(StringUtils.cutDateStr(statisticsModel.getEnd()));
        }
        if(StringUtils.isNoNullString(statisticsModel.getCode())){
            criteria.andNetroundEqualTo(Short.parseShort(statisticsModel.getCode()));
        }
        if (statisticsModel.getOrgcodelist().size() > 0 && !statisticsModel.getAreacode().equals("3300")){
            criteria.andOrgcodeIn(statisticsModel.getOrgcodelist());
        }
        orgnetinfoCriteria.setOrderByClause("workdate desc");
        return orgnetinfoMapper.selectByExample(orgnetinfoCriteria);
    }


    /**
     * 打印报表
     * @return
     */
    @Override
    public List<OrgNetInfoPrint> orgnetInfoPrints(){
        List<OrgNetInfoPrint> data = new ArrayList<>();
        OrgnetinfoCriteria orgnetinfoCriteria=new OrgnetinfoCriteria();
        orgnetinfoCriteria.createCriteria().getAllCriteria();
        List<Orgnetinfo> list = orgnetinfoMapper.selectByExample(orgnetinfoCriteria);
        for(Orgnetinfo netresult:list){
            OrgNetInfoPrint tmp = new OrgNetInfoPrint();
            tmp.setWorkdate(netresult.getWorkdate());
            tmp.setNetround(netresult.getNetround().toString());
            tmp.setOrgcode(netresult.getOrgcode());
            tmp.setBankcode(netresult.getBankcode());
            tmp.setAcctbank(netresult.getAcctbank());
            switch (netresult.getTrncode()){
                case "7001":
                    tmp.setTrncode("实时代收");
                    break;
                case "7003":
                    tmp.setTrncode("实时代付");
                    break;
                case "7007":
                    tmp.setTrncode("在线支付");
                    break;
                case "7009":
                    tmp.setTrncode("同名账户资金归筹");
                    break;
                case "7013":
                    tmp.setTrncode("实时缴费");
                    break;
                case "7101":
                    tmp.setTrncode("批量代收");
                    break;
                case "7102":
                    tmp.setTrncode("批量代付");
                    break;
            }
            PrtryinfoCriteria prtryinfoCriteria = new PrtryinfoCriteria();
            prtryinfoCriteria.createCriteria().andCodeEqualTo(netresult.getPrtry());
//            tmp.setTrncode(netresult.getTrncode());
            tmp.setPrtry(prtryinfoMapper.selectByExample(prtryinfoCriteria).get(0).getName());
            if (netresult.getDcflag()=="C"){
                tmp.setDcflag("贷记");
            }else if (netresult.getDcflag()=="D"){
                tmp.setDcflag("借记");
            }
            tmp.setAmount(netresult.getAmount().toString());
            data.add(tmp);
        }
        return data;
    }
}
